<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/1/30
  Time: 9:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>

    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui/css/layui.css">
</head>
<body class="childrenBody">
<!-- 搜索条件开始 -->
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>查询条件</legend>
</fieldset>
<form class="layui-form" method="post" id="searchFrm">

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">单号:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="rentId" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入出租单号" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">租客身份证号</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="idCard" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入身份证号" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">租房地址:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="address" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入车牌号" style="height: 30px;border-radius: 10px">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">开始时间:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="startTime" id="startTime" readonly="readonly"
                       class="layui-input layui-input-inline"
                       placeholder="请输入出租开始时间" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">结束时间:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="endTime" id="endTime" readonly="readonly"
                       class="layui-input layui-input-inline"
                       placeholder="请输入出租结束时间" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">操作人员:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="openName" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入操作人员" style="height: 30px;border-radius: 10px">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">出租状态:</label>
            <div class="layui-input-inline">
                <input type="radio" name="rentFlag" value="0" title="已归还">
                <input type="radio" name="rentFlag" value="1" title="未归还">
            </div>
        </div>
    </div>
    <div class="layui-form-item" style="text-align: center">
        <div class="layui-input-black" >
            <button type="button"
                    class="layui-btn layui-btn-normal layui-icon layui-icon-search layui-btn-radius layui-btn-sm"
                    lay-submit  lay-filter="doSearch" style="margin-top: 4px">查询
            </button>
            <button type="reset"
                    class="layui-btn layui-btn-warm layui-icon layui-icon-refresh layui-btn-radius layui-btn-sm"
                    style="margin-top: 4px">重置
            </button>
        </div>
    </div>

</form>
<!-- 搜索条件结束 -->
<table id="rentTable" lay-filter="houseTableFilter"></table>


<script id="barHouse" type="text/html">

    <%-- 房屋未归还显示编辑和删除按钮， 如果归还就不显示 --%>
    {{#  if(d.rentFlag == 1){ }}
    <a class="layui-btn layui-btn-xs layui-btn-radius" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs layui-btn-radius" lay-event="del">删除</a>
    {{# }else{ }}
    <a class="layui-btn layui-btn-green layui-btn-xs layui-btn-radius" lay-event="exportRent">导出出租单</a>
    {{# } }}

</script>



<!-- 添加和修改的弹出层-->
<div style="display: none;padding: 20px" id="saveOrUpdateDiv">
    <form class="layui-form" lay-filter="dataFrm" id="dataFrm">
        <div class="layui-form-item">
            <label class="layui-form-label">出租单号:</label>
            <div class="layui-input-block">
                <input type="text" name="rentId" lay-verify="required" readonly="readonly" placeholder="请输入出租单号"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">起租时间:</label>
                <div class="layui-input-inline">
                    <input type="text" name="beginDate" id="begindate" readonly="readonly" lay-verify="required" placeholder="请输入起租时间" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">还车时间:</label>
                <div class="layui-input-inline">
                    <input type="text" name="returnDate" id="returndate" readonly="readonly"  placeholder="请输入还车时间" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">身份证号:</label>
                <div class="layui-input-inline">
                    <input type="text" name="idCard" lay-verify="required" readonly="readonly" placeholder="请输入身份证号"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">地址:</label>
                <div class="layui-input-inline">
                    <input type="text" name="address" lay-verify="required" readonly="readonly"  placeholder="请输入车牌号" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">出租价格:</label>
                <div class="layui-input-inline">
                    <input type="text" name="price" lay-verify="required" placeholder="请输入出租价格" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center;padding-right: 120px">
                <button type="button"
                        class="layui-btn layui-btn-normal layui-btn-md layui-icon layui-icon-release layui-btn-radius"
                        lay-filter="doSubmit" lay-submit="">提交
                </button>
                <button type="reset"
                        class="layui-btn layui-btn-warm layui-btn-md layui-icon layui-icon-refresh layui-btn-radius">重置
                </button>
            </div>
        </div>
    </form>
</div>


<script type="text/javascript" src="${pageContext.request.contextPath}/static/layui/layui.js"></script>


<script>
    var rentTab;
    var rentIndex;
    var title;
    layui.use(['form', 'layer', 'jquery','table','laydate' ], function () {

        var form = layui.form, layer = layui.layer, $ = layui.jquery,table = layui.table,laydate = layui.laydate;

        laydate.render({
            elem:'#startTime'
            ,type:'datetime'
        })
        laydate.render({
            elem:'#endTime'
            ,type:'datetime'
        })

            rentTab =  table.render({
                elem:"#rentTable"
                ,url:"${pageContext.request.contextPath}/business/rentJson"
                ,cols:[[{field: 'rentId', title: '出租单号'}
                    ,{field: 'price', title: '出租价格'}
                    ,{field: 'beginDate', title: '出租开始时间'}
                    ,{field: 'returnDate', title: '出租结束时间'}
                    ,{field: 'idCard', title: '身份证号'}
                    ,{field: 'houseId', title: '房屋编号'}
                    ,{field: 'openName', title: '操作者'}
                    ,{field: 'rentFlag', title: '出租状态' ,templet:function(data) {
                            return data.rentFlag == 1 ? "未归还" :"<font color=red >已归还</font>"
                        }}
                    ,{field: 'createTime', title: '创建时间'}
                    ,{fixed: 'right', title:'操作', toolbar: '#barHouse',width:300}
                ]]
                ,page:true
                ,limits:[5,10,15]
                ,limit:5
            })

        //给查询按钮添加事件
        form.on('submit(doSearch)',function (data) {
            rentTab.reload({
                where:data.field
                ,page:{curr:1}
            })
            return false;
        })

        // 监听行工具栏
        table.on('tool(houseTableFilter)',function (obj) {
           
            if(obj.event == 'rent'){
                rent(obj.data)
            }else if(obj.event == 'exportRent'){
                location.href = "${pageContext.request.contextPath}/business/rentExport?rentId="+obj.data.rentId
            }
        })

        var openTable;
        function rent(data) {
            rentIndex = layer.open({
                type:1
                ,title:"房屋出租"
                ,area:['700px','500px']
                ,content:$('#saveOrUpdateDiv')
                ,success:function (index, layero) {
                    // 1.给时间控件赋值
                    laydate.render({
                        elem:'#beginDate'
                        ,value:new Date()
                        ,type:'datetime'
                    })
                    laydate.render({
                        elem:'#returnDate'
                        ,type:'datetime'
                    })

                    // 2.出租单号
                    $.post("${pageContext.request.contextPath}/business/zcCodeJson",function (r) {
                        $('[name="rentId"]').val(r)
                    })

                    // 3. 身份证
                    $("#identity1").val($("#identity").val())

                    // 4.车牌号，出租价格
                    $('[name="houseId"]').val(data.houseId)
                    $('[name="price"]').val(data.price)
                    $("[name=\"openname\"]").val($("#loginUsername").val())
                }
            })
        }

        // 监听提交按钮
        form.on('submit(doSubmit)',function (data) {
            $.post("${pageContext.request.contextPath}/business/addRentJson",data.field,function (r) {
                if(r.code == 0){
                    layer.msg(r.msg, {
                        offset: '15px'
                        ,icon: 1
                        ,time: 1000
                    })

                    // 关闭当前弹出层
                    layer.close(rentIndex)
                    // 刷新table数据
                    rentTab.reload({
                        page:{curr:1}
                    })
                }else{
                    layer.msg(r.msg, {
                        offset: '15px'
                        ,icon: 1
                        ,time: 1000
                    })
                }
            })
            return false;
        })
    })

</script>
</body>
</html>
